<?php

namespace app\common\ShaoHuoPay\Gateways\Wechat;

use app\common\ShaoHuoPay\tools\Str;
use app\common\ShaoHuoPay\tools\ShCollection;
use app\common\ShaoHuoPay\Gateways\Wechat\Gateway;
use app\common\ShaoHuoPay\Gateways\Wechat\WxTools;


class MpGateway extends Gateway
{
    /**
     * 微信公众号支付订单
     * @param $endpoint
     * @param array $payload
     * @return ShCollection|mixed
     * @throws \App\common\ShaoHuoPay\Exceptions\InvalidArgumentException
     */
    public function pay($endpoint, array $payload)
    {
        $payload['trade_type'] = $this->getTradeType();

        $payRequest = [
            'appId'     => $payload['appid'],
            'timeStamp' => strval(time()),
            'nonceStr'  => Str::random(),
            'package'   => 'prepay_id='.$this->preOrder('pay/unifiedorder', $payload)->prepay_id,
            'signType'  => 'MD5',
        ];
        $payRequest['paySign'] = WxTools::generateSign($payRequest, $this->config->get('key'));

        //Log::debug('Paying A JSAPI Order:', [$endpoint, $payRequest]);

        return new ShCollection($payRequest);
    }

    /**
     * 获取交易类型
     * @return mixed|string
     */
    protected function getTradeType()
    {
        return 'JSAPI';
    }
}
